WP7]Dynamic Color for theme awareness OR How to define a dynamic value for a property in a custom resource in App.xaml?

See this question about theme detection . That would get you a way to detect the theme, then use that information to set your background brush by replacing it in the app's resources.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I'm making an app for windows phone 7 (using Visual Studio 2010 Express for Windows Phone RTM), and i'm having some problems regarding theme (light/dark) awareness especially with colors. Basically I want to do the same thing that the phone does internally when using the staticresource colors/brushes. For example, if I declare: The Border will have a black background with the Dark Theme and White with the Light one.

So, I want to have a resource ... let's say MyBackgroundBrush, and will be used like: And, it will have, maybe blue for the Dark theme, and red for the light. So my question is, how can I define such resource and behavior? Silverlight windows-phone-7 link|improve this question asked Oct 15 '10 at 21:11Felipe Guajardo70110 80% accept rate.

See this question about theme detection. That would get you a way to detect the theme, then use that information to set your background brush by replacing it in the app's resources: App.Current.Resources. Remove("MyPhoneBrush"); App.Current.Resources.

Add("MyPhoneBrush", value); You could define both of the colored brushes with a name in xaml, then set one or the other as the runtime value of MyPhoneBrush. You'd probably want to set one of them as the default so things work at design time too...

Thanks for your answer. I think I understand the idea but i'm unable to test it right now. Where should I declare your code?

Will it work in App.xaml. Cs, like this: this.Current.Resources. Add("MyPhoneBrush", value);?

– Felipe Guajardo Oct 16 '10 at 5:55 App. Current is static, so you should be able to put that code pretty much anywhere. If you're basing stuff on one of the standard templates, you could put it in MainPage_Loaded, where (depending on the project) there should already be code referencing App.

– John Gardner Oct 18 '10 at 16:05 Thanks, I just made it work a while ago. One thing to note though, it might have been due to my rudimentary implementation, but it matters where the code is declared. For example, I had a texblock style definition in App.xaml.

Then I declared your code in App.xaml. Cs->Application_Launching(s,e). After that, I changed the Foreground property in my style to the static resource, but didn't work since (i guess) App.

Xaml gets parsed before the resource gets added. Still, I got it to work by overriding Foreground directly in the textblock with the staticresource. – Felipe Guajardo Oct 18 '10 at 16:27.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions